<template>
	<view>
		<view class="title">
			<view class="input_l">
				<uni-icons color="#9D9D9D" type="search" size="26"></uni-icons>
				<input @confirm="inputseacher" confirm-type="search" v-model="params.keyWord" placeholder-class="inputs"
					type="text" placeholder="输入客户、名称、电话搜索" />
			</view>
			<uni-datetime-picker type="date" v-model="single" @change="changeLog">
				<image class="logo" src="/static/time.png"></image>
			</uni-datetime-picker>
		</view>
		<view class="ulist">
			<view class="item" v-for="(item,index) in quotationList" :key="index" @click="godetail(item)">
				<view class="item_t">
					<view class="imgs">
						<image src="/static/ht.png" mode=""></image>
					</view>
					<view class="i_r">
						<view class="tit">{{item.customerName}}</view>
						<view class="order">单号：{{item.contractNumber}}</view>
					</view>
				</view>
				<view class="item_c">
					<view class="time">
						创建于：{{item.createTime}}
					</view>
					<view class="price">
						￥{{item.contractAmount |numberToCurrencyNo}}
					</view>
				</view>
				<view class="item_b">
					<view class="its" @click.stop="remove(item)">
						<image src="/static/sc.png" mode=""></image>
						删除
					</view>
					<view class="x"></view>
					<view class="its" @click.stop="goquotation(item)">
						<image src="/static/xg.png" mode=""></image>
						编辑
					</view>
					<view class="x"></view>
					<view class="its" @click.stop="contractPdf(item)">
						<image src="/static/pdf.png" mode=""></image>
						合同
					</view>
				</view>
			</view>
		</view>
		<view class="" style="height: 100rpx;">
		</view>
		<view class="kong" v-if="nolist">
			<image src="/static/kong.png" mode=""></image>
			<view class="ts">
				暂无信息
			</view>
		</view>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog :type="msgType" cancelText="取消" confirmText="确定" title="删除提示" content="你确定要删除当前合同吗!？"
				@confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		contractList,
		contractRemove,
		contractPdf
	} from '@/common/http.api.js';
	export default {
		data() {
			return {
				params: {
					pageNum: 1,
					pageSize: 10,
					keyWord: '',
					createTimeBegin: '',
					createTimeEnd: '',
				},
				nolist: false,
				quotationList: [],
				removeid: ''
			};
		},
		onLoad() {
			this.getList()
		},
		onReachBottom() {
			this.params.pageNum++
			this.getList()
		},
		methods: {
			contractPdf(item) {
					let that = this
					wx.showLoading({
						title: "资源下载中...",
					});
					const fileName = item.contractNumber+'-'+item.customerName
					//下载文件资源到本地
					wx.downloadFile({
						url:getApp().globalData.BASEURL+`/contract/pdf?id=${item.id}`, //后端返回的pdf路径
						header: {
							'Authorization': 'Bearer ' + uni.getStorageSync('token'),
							'token': uni.getStorageSync('token')
						},
						filePath: `${wx.env.USER_DATA_PATH}/${fileName}.pdf`, //自定义pdf文件名
						success: function(res) {
							const filePath = res.filePath || res.tempFilePath
							wx.openDocument({
								showMenu: true, //分享按钮
								filePath: filePath,
								success: function(res) {
									wx.hideLoading();
								}
							})
						}
					})
				
			},
			godetail(item) {
				uni.navigateTo({
					url: `/pages/index/contractdetail?id=${item.id}`
				})
			},
			remove(item) {
				this.removeid = item.id
				this.$refs.alertDialog.open()
			},
			dialogConfirm() {
				contractRemove({
					id: this.removeid
				}).then(res => {
					this.params.pageNum = 1
					this.quotationList = []
					this.getList()
				})
			},
			dialogClose() {
				this.$refs.alertDialog.close()
			},
			getList() {
				contractList(this.params).then(res => {
					this.quotationList.push(...res.rows)
					let total = Math.ceil(res.total / this.params.pageSize);
					this.nolist = total == 0
				})
			},
			inputseacher() {
				this.params.pageNum = 1
				this.quotationList = []
				this.getList()
			},
			changeLog(e) {
				this.params.createTimeBegin = e
				this.params.createTimeEnd = e
				this.params.pageNum = 1
				this.quotationList = []
				this.getList()
			},
			goquotation(item) {
				console.log(item);
				uni.navigateTo({
					url: `/pages/index/contract?id=${item.id}&&istype=1`
				})

			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #F7F8FA;
	}

	.kong {
		margin-top: 100rpx;
		text-align: center;

		image {
			width: 372rpx;
			height: 268rpx;
		}

		.ts {
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #D1D1D6;
		}
	}

	.title {
		width: 750rpx;
		height: 120rpx;
		background: #FFFFFF;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		padding: 25rpx 20rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
		position: sticky;
		top: 0%;

		.input_l {
			display: flex;
			width: 630rpx;
			height: 80rpx;
			background: #F9F9F9;
			border-radius: 60rpx 60rpx 60rpx 60rpx;
			align-items: center;
			padding-left: 20rpx;

			input {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				flex: 1;
			}
		}

		.logo {
			width: 60rpx;
			height: 60rpx;
		}
	}

	.ulist {
		.item {
			width: 710rpx;
			background: #FFFFFF;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			margin: 20rpx auto;
			padding: 30rpx 26rpx 26rpx 30rpx;
			box-sizing: border-box;

			.item_t {
				display: flex;

				.imgs {
					width: 80rpx;
					height: 80rpx;
					background: #F67220;
					border-radius: 40rpx;
					display: flex;
					justify-content: center;
					align-items: center;

					image {
						width: 52rpx;
						height: 52rpx;
						display: block;
					}
				}

				.i_r {
					margin-left: 20rpx;

					.tit {
						font-family: PingFang SC, PingFang SC;
						font-weight: bold;
						font-size: 32rpx;
						color: #000000;
					}

					.order {
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 28rpx;
						color: #757575;
						margin-top: 6rpx;
					}
				}

			}

			.item_c {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-top: 16rpx;
				padding-bottom: 30rpx;
				border-bottom: 1rpx solid #DADADA;

				.time {
					font-size: 24rpx;
					color: #979797;
				}

				.price {
					font-weight: 800;
					font-size: 40rpx;
					color: #F67220;
				}
			}

			.item_b {
				display: flex;
				align-items: center;
				margin-top: 26rpx;
				padding: 0rpx 40rpx;
				justify-content: space-between;

				.its {
					display: flex;
					align-items: center;

					image {
						width: 40rpx;
						height: 40rpx;
						margin-right: 14rpx;
					}

					font-family: PingFang SC,
					PingFang SC;
					font-weight: 500;
					font-size: 24rpx;
					color: #979797;
				}

				.x {
					width: 2rpx;
					height: 40rpx;
					background-color: #D1D1D6;
				}
			}
		}
	}
</style>